package com.example.demo;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    private List<String> userList = new ArrayList<>();
    public UserController() {
        userList.add("Alice");
        userList.add("Bob");
        userList.add("Charlie");
        userList.add("David");
        userList.add("Eve");
    }
    @GetMapping
    public List<String> listUsers() {
        return userList;
    }
@GetMapping("/{id}")
public String getuser(@PathVariable("id") Integer id) {

    return userList.get(id);
}

@GetMapping("/add/{name}")
public String createUser(@PathVariable("name") String name) {
//        user.setId(nextId++);
    userList.add(name);
    return name;
}
    @GetMapping("/delete/{id}")
    public Integer deleteUser(@PathVariable("id") Integer id) {
        userList.remove(id.intValue());
        return id;
    }

@GetMapping("/edit/{id}/{name}")
public Integer updateUser(@PathVariable("id") Integer id, @PathVariable("name") String name) {
    userList.set(id, name);
    return id;
}}
